﻿using System;
using System.Collections;
using System.Reflection;
using System.Xml;
using System.Text;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.EscapeRule;

namespace InfoJetSoft.Service.Worker
{
	//ServerNode，CodeNode相关。
	//ServerNode就是只那些需要PostBack进行更新的字段，ServerNode不需要更新整个View，使用Update_ValueChange命令。
	//CodeNode就是包含OnValidate和OnAfterChange事件的字段，会更新整个View。
	class XSNManifestServerNodeWorker
	{
        //CID 0076 //2008.07.31 //View view.
        public static bool isServerNode(View view, string nodeName)
		{
			if( view.Manifest.ErrorConditionList != null )
			{
				foreach( ErrorCondition errorCondition in view.Manifest.ErrorConditionList )
				{
					if( ExpressionUtil.exprContainingNode( errorCondition.Expression, nodeName ) )
					{
						return true;
					}
					if( errorCondition.ExpressionContext.Equals( "." ) )
					{
						string matchNode = ExpressionUtil.parseTargetNodeName( errorCondition.Match );
						if( matchNode.Equals( nodeName ) )
						{
							return true;
						}
					}
					else
					{
						string contextNode = ExpressionUtil.parseTargetNodeName( errorCondition.ExpressionContext );
						if( contextNode.Equals( nodeName ) )
						{
							return true;
						}
					}
				}
			}
			if( view.Manifest.DomEventHandlerList != null )
			{
				foreach( DOMEventHandler domEventHandler in view.Manifest.DomEventHandlerList )
				{
					string matchNode = ExpressionUtil.parseTargetNodeName( domEventHandler.Match );
					if( matchNode.Equals( nodeName ) )
					{
						return true;
					}
				}
			}
			if( view.Manifest.CalculatedFieldList != null )
			{
				foreach( CalculatedField calculatedField in view.Manifest.CalculatedFieldList )
				{
					//CID 0294 //2009.05.30 //Begin
					//CID 0301 //2009.06.04 //Begin
					if( calculatedField.isRelatedNode( nodeName ) )
					{
						return true;
					}
					//CID 0301 //2009.06.04 //End
					//CID 0294 //2009.05.30 //End
				}
			}
			if( view.Manifest.ConditionFormatTestList != null )
			{
				foreach( string test in view.Manifest.ConditionFormatTestList )
				{
					if( ExpressionUtil.exprContainingNode( test, nodeName ) )
					{
						if( test.StartsWith( "not(string(" ) && test.EndsWith( "))" ) )
						{
							//如果是类似not(string(my:五项目基本情况/my:实施内容))(软件及信息服务业项目表格.xsn)，
							//不认为my:实施内容是ServerNode。
						}
						else
						{
							return true;
						}
					}
				}
			}
			if( view.ExprBindingList != null )
			{
				foreach( string binding in view.ExprBindingList )
				{
					if( ExpressionUtil.exprContainingNode( binding, nodeName ) )
					{
						return true;
					}
				}
			}
            //CID 1380 //2012.10.07 //Begin
            if (view.Manifest.ErrorBlankList != null)
            {
                foreach (ErrorBlank errorBlank in view.Manifest.ErrorBlankList)
                {
                    string matchNode = ExpressionUtil.parseTargetNodeName(errorBlank.Match);
                    if (matchNode.Equals(nodeName))
                    {
                        return true;
                    }
                }
            }
            //CID 1380 //2012.10.07 //End
			return false;
		}

		//判断给定的nodeName是否可能有一个FormCode事件对应.
		public static bool isCodeNode( Manifest manifest, string nodeName )
		{
			//如果是Managed Form Code,使用InfoPathEventHandlerAttribute判断.
			if( manifest.ManagedAssembly != null && manifest.ManagedAssembly.ModelType != null )
			{
				Type modelType = manifest.ManagedAssembly.ModelType;
				foreach( MethodInfo method in modelType.GetMethods() )
				{
					foreach( Attribute attr in method.GetCustomAttributes(true) )
					{
						if( attr is Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute )
						{
							Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute eventAttr = (Microsoft.Office.Interop.InfoPath.SemiTrust.InfoPathEventHandlerAttribute) attr;
							if( eventAttr.MatchPath != null )
							{
								string matchNode = ExpressionUtil.parseTargetNodeName( eventAttr.MatchPath );
								if( matchNode.Equals( nodeName ) )
								{
									return true;
								}
							}
						}
					}
				}
			}
			//CID 0192 //2009.01.13 //Begin
			if( manifest.ManagedAssembly != null && manifest.ManagedAssembly.MainType != null )
			{
				Type mainType = manifest.ManagedAssembly.MainType;
				foreach( MethodInfo method in mainType.GetMethods() )
				{
					if( method.IsStatic )
					{
						foreach( Attribute attr in method.GetCustomAttributes(true) )
						{
							if( attr is System.ComponentModel.DescriptionAttribute )
							{
								System.ComponentModel.DescriptionAttribute eventAttr = (System.ComponentModel.DescriptionAttribute) attr;
								string desc = eventAttr.Description.Trim();
								if( desc.StartsWith( "InfoJet POM" ) && desc.IndexOf( "/" + nodeName ) > 0 )
								{
									return true;
								}
							}
						}
					}
				}
			}
			//CID 0192 //2009.01.13 //End
			//CID 0157 //2008.12.03 //Begin
			foreach( HandlerObject handlerObject in manifest.HandlerObjectList )
			{
				string matchNode = ExpressionUtil.parseTargetNodeName( handlerObject.Match );
				if( matchNode.Equals( nodeName ) )
				{
					return true;
				}
			}
			/*
			//如果是Script Form Code,根据节点名是否存在于procName的后端来判断.
			if( manifest.ScriptSource != null )
			{
				foreach( string procName in manifest.ScriptSource.ProcNameList )
				{
					if( procName.EndsWith( "_OnValidate" ) )
					{
						if( ScriptCodeUtil.maybeMsoxdProcName( procName, nodeName, "_OnValidate" ) )
						{
							return true;
						}
					}
					if( procName.EndsWith( "_OnAfterChange" ) )
					{
						if( ScriptCodeUtil.maybeMsoxdProcName( procName, nodeName, "_OnAfterChange" ) )
						{
							return true;
						}
					}
				}
			}
			*/
			//CID 0157 //2008.12.03 //End
			return false;
		}

		public static bool isRuleMatchNode( Manifest manifest, string nodeName )
		{
			if( manifest.DomEventHandlerList != null )
			{
				foreach( DOMEventHandler domEventHandler in manifest.DomEventHandlerList )
				{
					String targetNode = ExpressionUtil.parseTargetNodeName( domEventHandler.Match );
					if( targetNode.Equals( nodeName ) )
					{
						return true;
					}
				}
			}
			return false;
		}

		public static bool isConfigRefreshAfterUpdateNode( Manifest manifest, string nodeName )
		{
			return manifest.FormConfiguration.IsRefreshAfterUpdateNode( nodeName );
		}

		//CID 0028 //2008.06.21 //Begin
		public static bool isCFVFileAdapterSourcePathNoe( Manifest manifest, string nodeName )
		{
			return manifest.FormConfiguration.IsCFVFileAdapterSourcePathNode( nodeName );
		}
		//CID 0028 //2008.06.21 //End

	}
}